package com.cxf.windex.base.util;



import com.cxf.windex.entity.CityInfo;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.helper.StringUtil;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: a_master_repair_master
 * @description:
 * @author: chenxf
 * @create: 2022-02-18 09:29
 **/
public class FetchUtil {



    private static Map<String,String> cookie=new HashMap<>();

    String baseUri="http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2021/index.html";
    public FetchUtil(){
    }




    public void save() {


    }
    public List<CityInfo> fetchProvince() throws Exception {
        Document document = Jsoup.connect(baseUri)
                .ignoreContentType(true)
                .method(Connection.Method.GET)
                .userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.82 Safari/537.36")
                // .cookies(cookie)
                .get();
        //System.out.println(document.body().html());
        Elements infoElements = document.select("tr.provincetr>td>a");
        //System.out.println(infoElements.size());
        List<CityInfo> cityInfos=new ArrayList<>(infoElements.size());
        for(Element element:infoElements){
            String path=element.attr("href");
            CityInfo cityInfo=new CityInfo();
            cityInfo.setAreaCode(path.substring(0,path.indexOf(".")));
            cityInfo.setAreaName(element.text());
            cityInfo.setFetchUrl(baseUri.substring(0,baseUri.lastIndexOf("/")+1)+path);
            cityInfo.setAreaLevel(1);
            cityInfo.setStatus(0);
            cityInfos.add(cityInfo);
        }
        return cityInfos;
    }

    public List<CityInfo> fetchVillage(String url,Integer level) throws Exception {
        String trClass=".villagetr";
        Document document = Jsoup.connect(url)
                .ignoreContentType(true)
                .method(Connection.Method.GET)
                .userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.82 Safari/537.36")
                // .cookies(cookie)
                .get();

        Elements infoElements = document.select("tr"+trClass);

        List<CityInfo> cityInfos=new ArrayList<>(infoElements.size());
        for(Element element:infoElements){
            Element codeEl=element.select("td:nth-child(1)").first();
            Element villageCodeEl=element.select("td:nth-child(2)").first();
            Element nameEl=element.select("td:nth-child(3)").first();
            CityInfo cityInfo=new CityInfo();
            cityInfo.setAreaCode(codeEl.text());
            cityInfo.setVillageCode(villageCodeEl.text());
            cityInfo.setAreaName(nameEl.text());
            cityInfo.setStatus(1);
            cityInfo.setAreaLevel(level);
            cityInfos.add(cityInfo);
        }
        return cityInfos;
    }
    public List<CityInfo> fetchCity(String url,Integer level) throws Exception {
        String trClass="";
        switch (level){
            case 2:
                trClass=".citytr";
                break;
            case 3:
                trClass=".countytr";
                break;
            case 4:
                trClass=".towntr";
                break;
            case 5:
                return fetchVillage(url,level);
            default:
                return new ArrayList<>();
        }
        Document document = Jsoup.connect(url)
                .ignoreContentType(true)
                .method(Connection.Method.GET)
                .userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.82 Safari/537.36")
               // .cookies(cookie)
                .get();

        Elements infoElements = document.select("tr"+trClass);

        List<CityInfo> cityInfos=new ArrayList<>(infoElements.size());
        for(Element element:infoElements){
            Element codeEl=element.select("td:nth-child(1)").first();
            Element nameEl=element.select("td:nth-child(2)").first();

            CityInfo cityInfo=new CityInfo();
            cityInfo.setAreaCode(codeEl.text());
            cityInfo.setAreaName(nameEl.text());
            Elements hrefEl=codeEl.select("a");
            if(hrefEl.size()>0){
                String path=hrefEl.first().attr("href");
                cityInfo.setFetchUrl(url.substring(0,url.lastIndexOf("/")+1)+path);
                cityInfo.setStatus(0);
            }else {
                cityInfo.setStatus(1);
            }

            cityInfo.setAreaLevel(level);

            cityInfos.add(cityInfo);
        }
        return cityInfos;

    }

    public static void main(String[] args){
        String cityUrl="http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2021/33.html";
        String countryUrl="http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2021/33/3302.html";
        String townUrl="http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2021/33/02/330203.html";
        String villageUrl="http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2021/33/02/03/330203100.html";
        FetchUtil fetchUtil=new FetchUtil();
        try {
           // List<CityInfo> cityInfos= fetchUtil.fetchCity(cityUrl,2);
           // List<CityInfo> cityInfos= fetchUtil.fetchCity(countryUrl,3);
           // List<CityInfo> cityInfos= fetchUtil.fetchCity(townUrl,4);
            List<CityInfo> cityInfos= fetchUtil.fetchCity(villageUrl,5);
            cityInfos.stream().forEach(e->{
                System.out.println(e.getAreaCode()+"::"+e.getAreaName()+"::"+e.getFetchUrl());
            });

        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}
